package com.belano.controller;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.belano.catalog.PersonCatalog;
import com.belano.domain.People;
import com.belano.domain.Person;

@Component
@Path("people")
@Produces("application/xml")
public class PersonController {

    private PersonCatalog personCatalog;

    @Autowired
    public void setPersonCatalog(PersonCatalog personCatalog) {
        this.personCatalog = personCatalog;
    }

    @GET
    public People getAll() {
        return new People(personCatalog.getPeople());
    }

    @GET
    @Path("{id}")
    public Person getPerson(@PathParam("id") Long personId) {
        return personCatalog.getPerson(personId);
    }

    @POST   
    @Consumes(MediaType.APPLICATION_JSON)
    public Response savePerson(Person person) {
        person.setId(Person.personIdCounter++);
        personCatalog.savePerson(person);
        
        String result = "Person saved : " + person.getId();
        return Response.status(201).entity(result).build();        
    }

}
